package com.earogya.logic.patient_details;

import java.util.Calendar;
import java.util.List;

import org.apache.struts.ehr.patient_details.model.*;

import com.earogya.Constants;

import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.datatype.*;
import ca.uhn.hl7v2.model.v251.message.ADT_A01;
import ca.uhn.hl7v2.model.v251.segment.*;


/***
 * 
 * Create ADT A04 message based on the information given by the user.
 * Used to register a patient in the system.
 * 
 * @author Ureka
 *
 */
public class CreateADTA04 {
	private ADT_A01 adt;
	private PatientDetails patientDetails;
	
	public CreateADTA04(PatientDetails patientDetails){
		this.adt=new ADT_A01();
		this.patientDetails=patientDetails;
	}
	
	
	/***
	 * 
	 * Create the ADT A04 message
	 * 
	 * @param sendingFacility
	 * @return
	 * @throws HL7Exception
	 */
	public String createMessage(String sendingFacility) throws HL7Exception{
		this.addMessageHeader(sendingFacility);
		this.addPatientDetails();
		this.addPatientAddressDetails();
		this.addEmailAndTelephoneDetails();
		return adt.getMessage().encode();
	}
	

	/**
	 * 
	 * ADD the message header
	 * 
	 * @param sendingFacility
	 * @throws DataTypeException
	 */
	private void addMessageHeader(String sendingFacility) throws DataTypeException{
		adt.getMSH().getFieldSeparator().setValue("|");
		adt.getMSH().getEncodingCharacters().setValue("^~\\&");
		adt.getMSH().getDateTimeOfMessage().getTime().setValue(Calendar.getInstance().getTime());
		adt.getMSH().getMessageType().getMsg1_MessageCode().setValue("ADT");
		adt.getMSH().getMessageType().getTriggerEvent().setValue("A04");
		adt.getMSH().getSendingApplication().getUniversalID().setValue(Constants.SENDING_APPLICATION);
		adt.getMSH().getVersionID().getVersionID().setValue(Constants.HL7_VERSION);
		adt.getMSH().getSendingFacility().getUniversalID().setValue(sendingFacility);
		adt.getMSH().getReceivingApplication().getUniversalID().setValue(Constants.RECEIVING_APPLICATION_PATIENT_DETAILS_SERVICE);
		adt.getMSH().getReceivingFacility().getUniversalID().setValue(sendingFacility);
	}
	
	/**
	 * 
	 * Add the demographic details for the message
	 * 
	 * @throws DataTypeException
	 */
	private void addPatientDetails() throws DataTypeException {
		PID pid=adt.getPID();
		PD1 pd1=adt.getPD1();
		Patient patient=patientDetails.getPatient();
		XPN pName=adt.getPID().getPatientName(0);
		pName.getPrefixEgDR().setValue(patient.getPrefix());
		pName.getGivenName().setValue(patient.getGivenName());
		pName.getFamilyName().getSurname().setValue(patient.getFamilyName());
		pid.getPatientIdentifierList(0).getIDNumber().setValue(patient.getNicNo());
		if(patient.getBirthTime()!= null){pid.getDateTimeOfBirth().getTime().setValue(patient.getBirthTime());}
		pid.getBirthPlace().setValue(patient.getBirthPlaceAddress());
		pid.getAdministrativeSex().setValue(patient.getGender());
		pid.getMaritalStatus().getText().setValue(patient.getMaritialStatus());
		pid.getReligion().getText().setValue(patient.getReligiousAffliationId().toString());
		pid.getEthnicGroup(0).getText().setValue(patient.getEthnicGroupId().toString());
		pid.getPatientDeathIndicator().setValue("0");
		
	}
	
	/**
	 * 
	 * Add the patient address details to the message
	 * 
	 * @throws DataTypeException
	 */
	private void addPatientAddressDetails() throws DataTypeException {
		PatientAddress pAddressList=patientDetails.getPatientAddressList();
		if(!(pAddressList.getHouseNumber().equals(""))||!(pAddressList.getStreet().equals(""))||!(pAddressList.getCity1().equals(""))
				||!(pAddressList.getCity2().equals(""))||!(pAddressList.getCountry().equals("0"))){
		PID pid=adt.getPID();
			pid.getPatientAddress(0).getOtherDesignation().setValue(pAddressList.getCity1());
			pid.getPatientAddress(0).getCountry().setValue(pAddressList.getCountry());
			pid.getPatientAddress(0).getStreetAddress().getDwellingNumber().setValue(pAddressList.getHouseNumber());
			pid.getPatientAddress(0).getCity().setValue(pAddressList.getCity2());
			pid.getPatientAddress(0).getStreetAddress().getStreetName().setValue(pAddressList.getStreet());
		}	
	}

	/**
	 * 
	 * Add the patient email and telephone details to the message
	 * 
	 * @throws DataTypeException
	 */
	private void addEmailAndTelephoneDetails() throws DataTypeException {
		try{
		PID pid=adt.getPID();
		List<PatientTelephone> patientTelephoneList=patientDetails.getPatientTelephoneList();
		List<PatientEmail> patientEmailList=patientDetails.getPatientEmailList();
		for(int i=0;i<patientTelephoneList.size();i++){
			pid.getPhoneNumberHome(i).getTelephoneNumber().setValue(patientTelephoneList.get(i).getTelephoneNo());
		}
		for(int i=0;i<patientEmailList.size();i++){
			pid.getPhoneNumberHome(i).getEmailAddress().setValue(patientEmailList.get(i).getEmail());
		}
		}
		catch(Exception e){
			e.printStackTrace();
		}
	}
}
